package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.db.Kj;
import com.apps.ftapp.db.LessonChild;
import com.apps.ftapp.model.MyLessonListModel;
import com.apps.ftapp.util.L;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyli on 2017/10/8.
 */

public class MyLessonListAdapter extends BaseAdapter implements View.OnClickListener {


    private Context context;
    private List<MyLessonListModel.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private String lessonId;
    private String type;


    private List<LessonChild> lessonList = new ArrayList<>();
    private List<Kj> kjList = new ArrayList<>();

    private MyClickListener mListener;

    @Override
    public void onClick(View v) {

        mListener.clickListener(v);
    }


    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }


    public MyLessonListAdapter(Context context, List<MyLessonListModel.DataBean> dataList, String id, MyClickListener listener, String type) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);
        this.mListener = listener;
        lessonId = id;
        this.type = type;

        lessonList = DataSupport.where("lessonid = ?", lessonId + "").find(LessonChild.class);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }

    public void addRes(List<MyLessonListModel.DataBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        MyLessonListModel.DataBean bean = dataList.get(i);
        kjList = DataSupport.where("lessonid = ?", bean.getId()).find(Kj.class);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_my_lesson_list_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        StringBuffer sb = new StringBuffer();
        if (null != bean.getTeachers() && bean.getTeachers().size() > 0) {
            for (MyLessonListModel.DataBean.TeachersBean b : bean.getTeachers()) {
                sb.append(b.getNickname() + " ");
            }
        }
        viewHolder.lessonLaoshi.setText("上课老师：" + sb.toString());

        if (bean.getStartTime().equals("1970-01-01 08:00")) {
            viewHolder.lessonTime.setText("上课时间：随时可以观看");

        } else {
            viewHolder.lessonTime.setText("上课时间：" + bean.getStartTime() + "至" + bean.getEndTime());
        }

        viewHolder.lessonStatus.setText(bean.getTbutton());
        viewHolder.lessonTitle.setText(bean.getTitle());

        viewHolder.lessonIgk.setText(bean.getText().getText());

        //开课状态（1 为开课中，2 为未开课，3 为已结课）


        if (bean.getTbutton().equals("直播中")) {
            viewHolder.lessonStatus.setBackgroundResource(R.drawable.activity_my_error_subject_red_btn2);
        } else if (bean.getTbutton().equals("回放课")) {
            viewHolder.lessonStatus.setBackgroundResource(R.drawable.activity_my_error_subject_rec_btn1);
        } else if (bean.getTbutton().equals("未开课")) {
            viewHolder.lessonStatus.setBackgroundResource(R.drawable.activity_my_error_subject_rec_btn);

        }
        viewHolder.lessonStatus.setText(bean.getTbutton());

        viewHolder.relativeLayout.setTag(i);
        viewHolder.relativeLayout.setOnClickListener(this);
        viewHolder.huancunkecheng.setOnClickListener(this);
        viewHolder.huancunkecheng.setTag(i);


        if (null != lessonList && lessonList.size() > 0) {

            L.i("main", "===========");

            for (LessonChild lesson : lessonList) {


                if (lesson.getChildId().equals(bean.getId())) {
                    viewHolder.huancunkecheng.setText("查看视频");
                    L.i("main", "=====11111======");
                    break;
                }


            }


        } else {
            viewHolder.huancunkecheng.setText("缓存课程");
        }

        if (type.equals("normal")) {
            viewHolder.huancunkecheng.setVisibility(View.GONE);
        }
        if ("1".equals(bean.getIfkj())) {
            viewHolder.huancunkejian.setVisibility(View.VISIBLE);
            if (kjList.size() > 0 && null != kjList) {
                viewHolder.huancunkejian.setText("查看课件");
            } else {
                viewHolder.huancunkejian.setText("缓存课件");
            }
        } else {
            viewHolder.huancunkejian.setVisibility(View.GONE);
        }

        viewHolder.huancunkejian.setOnClickListener(this);
        viewHolder.huancunkejian.setTag(i);

        return view;
    }


    class ViewHolder {
        TextView lessonStatus;
        TextView lessonTitle;
        TextView lessonLaoshi;
        TextView lessonTime;
        TextView huancunkecheng;
        TextView huancunkejian;
        TextView lessonIgk;
        RelativeLayout relativeLayout;

        public ViewHolder(View view) {
            lessonStatus = view.findViewById(R.id.lesson_status);
            lessonTitle = view.findViewById(R.id.lesson_title);
            lessonLaoshi = view.findViewById(R.id.lesson_laoshi);
            lessonTime = view.findViewById(R.id.lesson_time);
            huancunkecheng = view.findViewById(R.id.huancunkecheng);
            huancunkejian = view.findViewById(R.id.huancunkejian);
            lessonIgk = view.findViewById(R.id.lesson_igk);
            relativeLayout = view.findViewById(R.id.top_container);

        }
    }

}
